package com.tledu.xpath;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class XPath01 {
    private static Document document;
    private static XPath xPath;

    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
        init();


    }

    public static void init() throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
        // 1. 初始化
        //      1.1 初始化Document对象。 Document对应的就是要解析的文档(对于初始化的过程，大家不需要背下来)
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        // 不进行校验
        documentBuilderFactory.setValidating(false);
        // 创建了这个对象之后，就可以读取xml文件，获取Document
        DocumentBuilder db = documentBuilderFactory.newDocumentBuilder();
        // 读取到对应的文件. 这个对象就承载xml数据
        document = db.parse(new FileInputStream("F:\\生产实习\\数据可视化\\demo\\webMagic-19\\src\\main\\resources\\rss.xml"));
        //      1.2 初始化xpath
        XPathFactory xPathFactory = XPathFactory.newInstance();
        // 创建xpath，后续就可以通过xpath解析对应的xml文档了
        xPath = xPathFactory.newXPath();

//        getPriceBook();
//        getLastStudentName();
//        wildcard();
        hasChild();
    }

    public static void path() throws XPathExpressionException {
        // 2. 处理xml文档了，获取所有节点
//        NodeList nodeList = (NodeList) xPath.evaluate("/rss/channel//*", document, XPathConstants.NODESET);
        //通过@来指代对应的属性，做属性的选择
        // / 选择直接子代
        // // 选择所有后代
        // . 当前目录
        // .. 上级目录
        // @ 选择属性
        NodeList nodeList = (NodeList) xPath.evaluate("//*[@name='stu1']", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.println(nodeList.item(i).getNodeName());
        }
    }

    public static void getPriceBook() throws XPathExpressionException {
        // text() 获取内部的文本内容，
        // 价格大于35的book中的title标签里面的text值
        NodeList nodeList = (NodeList) xPath.evaluate("//book[price > 35]/title/text()", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            // getNodeValue() 可以获取标签的值
            System.out.println(nodeList.item(i).getNodeValue());
        }
    }

    // last() 获取最后一个元素
    public static void getLastStudentName() throws XPathExpressionException {

        NodeList nodeList = (NodeList) xPath.evaluate("/rss/college//student[last()]", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            // getNodeValue() 可以获取标签的值
            System.out.println(nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue());
        }
    }

    public static void wildcard() throws XPathExpressionException {
        // node() 匹配任意类型标签，[@*] 这标签必须要包含一个属性
        NodeList nodeList = (NodeList) xPath.evaluate("//node()[@*]", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            // getNodeValue() 可以获取标签的值
            System.out.println(nodeList.item(i).getNodeName());
        }
    }

    public static void hasChild() throws XPathExpressionException {
        // node() 匹配任意类型标签，[*] 这标签必须要包含一个子标签
        NodeList nodeList = (NodeList) xPath.evaluate("//node()[*]", document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            // getNodeValue() 可以获取标签的值
            System.out.println(nodeList.item(i).getNodeName());
        }
    }
}
